package com.laolang.pluto.framework.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import java.time.LocalDateTime;
import org.apache.ibatis.reflection.MetaObject;

/**
 * 自动填充创建时间与更新时间
 *
 * @author laolang
 * @date 2021-07-2021/7/31 18:03
 */
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_TIME = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = getFieldValByName(CREATE_TIME, metaObject);
        if (null == createTime) {
            LocalDateTime date = LocalDateTime.now();
            setFieldValByName(CREATE_TIME, date, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime date = LocalDateTime.now();
        setFieldValByName(UPDATE_TIME, date, metaObject);
    }
}
